package com.emser.unc.client.presenter;


import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.event.MenuEvent;
import com.emser.unc.client.helper.RPCCall;
import com.emser.unc.client.service.SellerServiceAsync;
import com.emser.unc.serializable.SellerDTO;
import com.emser.unc.serializable.UserAccountDTO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class SellerPopupPresenter implements Presenter{
	private final Display display;
	private SimpleEventBus eventBus; 
	private SellerDTO sellerDTO;
	private SellerServiceAsync rpcServiceSeller;
	private UserAccountDTO userA;
	
	public interface Display {	
	    Widget asWidget();
	    HasValue<String> getName();
	    HasValue<String> getLastName();
	    HasValue<String> getTelephone();
	    HasValue<String> getAddress();
	    HasValue<String> getZP();
	    HasValue<String> getCity();
	    HasValue<String> getUserName();
	    HasValue<String> getPassword();
	    HasClickHandlers getSaveButton();
	    HasClickHandlers getCancelButton();
	    void setTextPopup(String text);
	    void enableSave(boolean flag);
	    void disableUserName();
	    void disableName();
	    void disableLastName();
	    void setVisibeWName(boolean visible);
	    void setVisibeWLastName(boolean visible);
	    void setVisibeWTel(boolean visible);
	    void setVisibeWAddress(boolean visible);
	    void setVisibeWUserName(boolean visible);
	    void setVisibeWUserPass(boolean visible);
	    void setVisibeWCP(boolean visible);
	    void setVisibeWCity(boolean visible);
	    void hide();
	    String getTextPopup();
	  }
	public SellerPopupPresenter( SimpleEventBus eventBus,SellerServiceAsync rpcServiceSeller, Display display) { 
		this.eventBus = eventBus;
		this.rpcServiceSeller = rpcServiceSeller;
		this.display = display;
		this.sellerDTO = new SellerDTO();
		bind();
	}
	
	private void bind() {
		this.display.getSaveButton().addClickHandler(new ClickHandler() {   
	      public void onClick(ClickEvent event) {
	    	  display.enableSave(false);
	    	  if(display.getTextPopup().equals(CloudMobile.constants.titleEditSeller()))
	    		  doEdit();
	    	  else
	    		  doAdd();
	      }	
	    });
		this.display.getCancelButton().addClickHandler(new ClickHandler() {   
		      public void onClick(ClickEvent event) {
		    	  doClose();
		      }	
		});	
	}
	private void doClose() {
		display.hide();	
	}
	
	private void doEdit() {	
		disableWarningFields();
		if(!validateFields()){
			display.enableSave(true);
			return;
		}
		sellerDTO.setUserPassword(display.getPassword().getValue());
		sellerDTO.setFirstName(display.getName().getValue());
		sellerDTO.setLastName(display.getLastName().getValue());
		sellerDTO.setTelephone(display.getTelephone().getValue());
		sellerDTO.setAddress(display.getAddress().getValue());
		sellerDTO.setCity(display.getCity().getValue());
		sellerDTO.setZipCode(display.getZP().getValue());
		new RPCCall<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				display.hide();
				Window.alert(CloudMobile.constants.eEditSeller());	
				display.enableSave(true);
			}
			@Override
			public void onSuccess(Boolean result) {
				display.hide();
				if(result.booleanValue())
					eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuSellers()));							
				else
					Window.alert(CloudMobile.constants.eServerEditSeller());
				display.enableSave(true);
			}
			@Override
			protected void callService(AsyncCallback<Boolean> cb) {
				rpcServiceSeller.updateSeller(userA.getName(),sellerDTO, cb);		
			}
		}.retry(2);	
	}
	private void disableWarningFields() {
		display.setVisibeWName(false);
		display.setVisibeWLastName(false);
		display.setVisibeWUserName(false);
		display.setVisibeWUserPass(false);
		display.setVisibeWTel(false);
		display.setVisibeWAddress(false);
	}

	private void doAdd() {
		disableWarningFields();
		boolean vfield = validateFields();
		if(!vfield){
			display.enableSave(true);
			return;
		}
		sellerDTO.setUserName(display.getUserName().getValue());
		sellerDTO.setUserPassword(display.getPassword().getValue());
		sellerDTO.setFirstName(display.getName().getValue());
		sellerDTO.setLastName(display.getLastName().getValue());
		sellerDTO.setTelephone(display.getTelephone().getValue());
		sellerDTO.setAddress(display.getAddress().getValue());
		sellerDTO.setCity(display.getCity().getValue());
		sellerDTO.setZipCode(display.getZP().getValue());
		new RPCCall<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				display.hide();
				Window.alert(CloudMobile.constants.eAddSeller());
			}
			@Override
			public void onSuccess(Boolean result) {
				display.hide();
				if(result.booleanValue()){
					eventBus.fireEvent(new MenuEvent(CloudMobile.constants.menuSellers()));	
				}
				else
					Window.alert(CloudMobile.constants.eServerAddSeller());	
				display.enableSave(true);
			}
			@Override
			protected void callService(AsyncCallback<Boolean> cb) {
				rpcServiceSeller.addSeller(userA.getName(), sellerDTO, cb);	
			}
		}.retry(2);
	}
	
	private void setValuesDisplay(SellerDTO seller){
		sellerDTO = seller;
		display.getUserName().setValue(seller.getUserName());
		display.getPassword().setValue(seller.getUserPassword());
		display.getName().setValue(seller.getFirstName());
		display.getLastName().setValue(seller.getLastName());
		display.getAddress().setValue(seller.getAddress());
		display.getTelephone().setValue(seller.getTelephone());	
		display.getZP().setValue(seller.getZipCode());
		display.getCity().setValue(seller.getCity());
	}
	
	@Override
	public void go(HasWidgets container) {}

	public void configPopup(UserAccountDTO userA,String title){
		this.userA = userA;
		display.setTextPopup(title);
	}
	public void configPopup(UserAccountDTO userA,String title,SellerDTO seller){
		this.userA = userA;
		display.disableUserName();
		display.disableName();
		display.disableLastName();
		display.setTextPopup(title);
		setValuesDisplay(seller);
	}
	
	public boolean validateFields(){
		boolean validFields = true;
		if (display.getName().getValue().isEmpty()){
			display.setVisibeWName(true);
			validFields = false;
		}
	    if (display.getLastName().getValue().isEmpty()){
	    	display.setVisibeWLastName(true);
	    	validFields = false;
	    }
	    if (display.getUserName().getValue().isEmpty() ){
	    	display.setVisibeWUserName(true);
	    	validFields = false;
		}
	    if (display.getPassword().getValue().isEmpty() ){
	    	display.setVisibeWUserPass(true);
	    	validFields = false;
		}
	    if ((!display.getTelephone().getValue().matches("[0-9]{1,20}"))){
	    	display.setVisibeWTel(true);
	    	validFields = false;
		}
	    if (display.getAddress().getValue().isEmpty()){
	    	display.setVisibeWAddress(true);
	    	validFields = false;
		}
	    if (display.getCity().getValue().isEmpty()){
	    	display.setVisibeWCity(true);
	    	validFields = false;
		}
	    if (display.getZP().getValue().isEmpty()){
	    	display.setVisibeWCP(true);
	    	validFields = false;
		}
	    return validFields;
	    
	}

}
